/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.pessoal;

import br.com.saber.certificacao.beansjsf.interfacebean.BeanJSFInterface;
import br.com.saber.certificacao.beansjsf.login.LoginManager;
import br.com.saber.certificacao.beansjsf.semescopo.SelectItemManager;
import br.com.saber.certificacao.ejbs.stateless.interfaces.UsuarioBeanLocal;
import br.com.saber.certificacao.modelos.Usuario;
import br.com.saber.certificacao.modelos.validadores.exceptions.generica.ValidacaoExcecao;
import br.com.saber.certificacao.utils.Criptografia;
import br.com.saber.certificacao.utils.UtilMetodos;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author gbvbahia
 */
@ManagedBean(name = "dadosPessoaisManager")
@ViewScoped
public class DadosPessoaisManager implements BeanJSFInterface {

    @EJB
    private UsuarioBeanLocal usuarioBean;

    @ManagedProperty("#{loginManager}")
    private LoginManager loginManager;

    @ManagedProperty("#{selectItemManager}")
    private SelectItemManager selectItemManager;

    private Usuario usuario;

    private String senhaAtual;

    private String senhaAtual2;

    private String senha;

    /** Creates a new instance of DadosPessoaisManager */
    public DadosPessoaisManager() {
    }

    //====================
    // Iniciadores
    //====================
    @Override
    @PostConstruct
    public void init() {
        this.usuario = loginManager.getUsuario();
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "DadosPessoaisManager.init() executado!");
    }

    @Override
    @PreDestroy
    public void end() {
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "DadosPessoaisManager.end() executado!");
    }

    //====================
    //Métodos de Negócio
    //====================
    public void salvarDados() {
        try {
            this.usuarioBean.salvarUsuario(usuario, UtilMetodos.getURL(FacesContext.getCurrentInstance()));
            UtilMetodos.messageFactoringFull("usuarioCadastradoOk2", FacesMessage.SEVERITY_INFO, FacesContext.getCurrentInstance());
        } catch (ValidacaoExcecao v) {
            if (!StringUtils.isBlank(v.getAtributoName())) {
                UtilMetodos.messageFactoringFull(UtilMetodos.getResourceBundle(v.getMessage(), FacesContext.getCurrentInstance()), null, v.getAtributoName(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            } else {
                UtilMetodos.messageFactoringFull(v.getMessage(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            }
        }
    }

    public void alterarSenha() {
        if (!StringUtils.equals(senhaAtual, senhaAtual2)) {
            UtilMetodos.messageFactoringFull("alteracaoSenhasDiferentes", FacesMessage.SEVERITY_WARN, FacesContext.getCurrentInstance());
            senhaAtual = "";
            senhaAtual2 = "";
            return;
        } else if (!this.usuarioBean.criptografarSenha(senha, usuario.getRoles()).equals(usuario.getSenha())) {
            UtilMetodos.messageFactoringFull("alteracaoSenhaSenhaIncorreta", FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            return;
        } else {
            try {
                this.usuario.setSenha((Criptografia.encodePassword(senhaAtual, usuario.getRoles())));
                this.usuarioBean.salvarUsuario(usuario, UtilMetodos.getURL(FacesContext.getCurrentInstance()));
                UtilMetodos.messageFactoringFull("alteracaoSenhaAlterada", FacesMessage.SEVERITY_INFO, FacesContext.getCurrentInstance());
                senhaAtual = "";
                senhaAtual2 = "";
                senha = "";
                return;
            } catch (ValidacaoExcecao v) {
                if (!StringUtils.isBlank(v.getAtributoName())) {
                    UtilMetodos.messageFactoringFull(UtilMetodos.getResourceBundle(v.getMessage(), FacesContext.getCurrentInstance()), null, v.getAtributoName(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
                } else {
                    UtilMetodos.messageFactoringFull(v.getMessage(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
                }
            }
        }
    }

    //====================
    //Table Actions
    //====================
    //====================
    //SelectItem
    //====================
    //=========================
    //Getters AND Setters
    //=========================
    public LoginManager getLoginManager() {
        return loginManager;
    }

    public void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }

    public SelectItemManager getSelectItemManager() {
        return selectItemManager;
    }

    public void setSelectItemManager(SelectItemManager selectItemManager) {
        this.selectItemManager = selectItemManager;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public String getSenhaAtual() {
        return senhaAtual;
    }

    public void setSenhaAtual(String senhaAtual) {
        this.senhaAtual = senhaAtual;
    }

    public String getSenhaAtual2() {
        return senhaAtual2;
    }

    public void setSenhaAtual2(String senhaAtual2) {
        this.senhaAtual2 = senhaAtual2;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }
}
